package com.example.administrator.anyview;

import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.anyview.fragments.CenterFragment;
import com.example.administrator.anyview.fragments.HomeFragment;
import com.example.administrator.anyview.fragments.LookForFragment;
import com.example.administrator.anyview.fragments.ResidentFragment;
import com.example.administrator.anyview.my_views.NoSlideViewPager;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    NoSlideViewPager mNoSlideViewPager;

    LinearLayout mLinearLayout;

    TextView mHomeTab1;
    TextView mHomeTab2;
    TextView mHomeTab3;
    TextView mHomeTab4;

    MyFragmentAdapter mAdapter;

    int mCurrPage = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initEvents();
    }

    private void initViews() {
//        final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.loadingLayout);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
//                        linearLayout.setVisibility(View.GONE);
                    }
                });
            }
        }).start();

        mNoSlideViewPager = (NoSlideViewPager) findViewById(R.id.main_viewPager);
        mLinearLayout = (LinearLayout) findViewById(R.id.main_tabs);

        mHomeTab1 = (TextView) findViewById(R.id.home_tab_1);
        mHomeTab2 = (TextView) findViewById(R.id.home_tab_2);
        mHomeTab3 = (TextView) findViewById(R.id.home_tab_3);
        mHomeTab4 = (TextView) findViewById(R.id.home_tab_4);

        ArrayList<Fragment> mFragments = new ArrayList<>();

        HomeFragment homeFragment = new HomeFragment();
        ResidentFragment residentFragment = new ResidentFragment();
        LookForFragment lookForFragment = new LookForFragment();
        CenterFragment centerFragment = new CenterFragment();

        mFragments.add(homeFragment);
        mFragments.add(residentFragment);
        mFragments.add(lookForFragment);
        mFragments.add(centerFragment);

        mAdapter = new MyFragmentAdapter(getSupportFragmentManager(), mFragments);
        mNoSlideViewPager.setOffscreenPageLimit(4);

        mNoSlideViewPager.setAdapter(mAdapter);

        mNoSlideViewPager.setCurrentItem(0);

        mHomeTab1.setSelected(true);
        mHomeTab1.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.color_0786e7));

    }

    private void initEvents() {
        mHomeTab1.setOnClickListener(this);
        mHomeTab2.setOnClickListener(this);
        mHomeTab3.setOnClickListener(this);
        mHomeTab4.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.home_tab_1:
                switchPage(0);
                break;
            case R.id.home_tab_2:
                switchPage(1);
                break;
            case R.id.home_tab_3:
                switchPage(2);
                break;
            case R.id.home_tab_4:
                switchPage(3);
                break;
            default:
                break;
        }
    }

    private void switchPage(int position) {
        if (position != mCurrPage){
            TextView mTv1 = (TextView) mLinearLayout.getChildAt(mCurrPage);
            mTv1.setSelected(false);
            mTv1.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.color_666666));

            mCurrPage = position;
            mNoSlideViewPager.setCurrentItem(mCurrPage);
            mAdapter.notifyDataSetChanged();
            TextView mTv2 = (TextView) mLinearLayout.getChildAt(mCurrPage);
            mTv2.setSelected(true);
            mTv2.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.color_0786e7));
            switch (position){
                case 0:
                    mHomeTab1.setSelected(true);
                    break;
                case 1:
                    mHomeTab1.setSelected(true);
                    break;
                case 2:
                    mHomeTab1.setSelected(true);
                    break;
                case 3:
                    mHomeTab1.setSelected(true);
                    break;
                default:
                    break;
            }
        }
    }
}

